<?php
	session_start();
	if(!$_SESSION['login'] == 1 && $_SESSION['sessionid'] !== session_id()) {
		header("Location: ./");
		exit;
	}
	
	if($_SESSION['priv'] < 3) {
		$lang = $_SESSION['lang'];
		require_once("include/lang." . $lang . ".php");
		echo ERR_PRIV;
		exit;
	}
	
	if(isset($_GET['mode'])) {
		if($_GET['mode'] == "del") {
			ob_start();
			if(empty($_GET['id'])) {
				echo "Missing ID";
				exit;
			}
			require_once("include/functions.php");
			$id = $_GET['id'];
			$groups->remove($id);
			header("Location: groups.php");
			ob_end_flush();
			exit;
		}
	}
	
	if(isset($_POST['sub'])) {
		ob_start();
		$group = $_POST['group'];
		$lang = $_SESSION['lang'];
		require_once("include/lang." . $lang . ".php");
		if(empty($group)) {
			echo ERR_GROUP_ENTER;
			exit;
		}
		require("include/functions.php");
		$groups->add_group($group);
		header("Location: groups.php");
		ob_end_flush();
		exit;
	}
	
	include("include/class.mysql.php");
	include("include/layout.php");
			
	html_start();
?>

		<br />
		<span class="conin">
				<table class="group_add">
					<tr style="border-bottom:1px dotted #5a4d4d;background-color:#F0F0F0;">
						<td style="padding:5px 5px;"><b><?=GROUPS_ADD;?></b></td>
					</tr>
					<tr style="border-bottom:1px dotted #5a4d4d;background-color:white;">
						<td style="padding:5px 5px;">
							<form action="groups.php" method="post">
							<?=GROUPS_NAME;?>:
							<br />
							<input type="text" name="group" style="width:175px;" />
							<input type="submit" name="sub" class="sub" value="<?=ADDR_ADD;?>" />
							</form>
						</td>
					</tr>
				</table>
				<br />
				<table class="groups">
					<tr style="border-bottom:1px dotted #5a4d4d;background-color:#F0F0F0;">
						<td style="padding:5px 5px;width:37%;"><b><?=CONT_GROUP;?></b></td>
						<td style="width:58%;"><b><?=GROUPS_MEMBERS;?></b></td>
						<td style="width:5%;">&nbsp;</td>
					</tr>
				<?php
					$MySQL->conn();
					$sql = mysql_query("SELECT * FROM " . DB_PREFIX . "groups ORDER BY group_name ASC") or die(mysql_error());
					$tab_color = array("#F0F0F0", "#FDFDFD");
					$i = 1;
					while($row = mysql_fetch_array($sql)) {
				?>
					<tr style="border-bottom:1px dotted #5a4d4d;background-color:<?=$tab_color[$i % 2];?>">
						<td style="padding:5px 5px;"><?=strip_tags($row['group_name']);?></td>
						<td style="text-align:left;">
							<?php
								$sql2 = mysql_query("SELECT uname FROM " . DB_PREFIX . "users WHERE user_group = '" . $row['group_name'] . "'");
								$num = mysql_num_rows($sql2);
								$x = 0;
								while($member = mysql_fetch_array($sql2)) {
									echo $member['uname'];
									if(!$x <= $num && $num-$x !== 1) {
										echo ", ";
									}
									$x++;
								}
							?>
						</td>
						<td style="text-align:left;"><a href="javascript:if(confirm('<?=USERS_SURE;?> <?=strip_tags($row['group_name']);?>?\n<?=GROUPS_CONFIRM;?>')){location.href='groups.php?mode=del&id=<?=$row['id'];?>';}"><img src="img/del.png" style="border:0px;" alt="" /></a></td>
					</tr>
				<?php
					$i++;
					}
					$MySQL->close();
				?>
				</table>
				<br />
		</span>
<?php
	html_end();
?>